﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using DGSFramework;

namespace Framework
{
    public partial class ManageAccount : System.Web.UI.Page
    {
        DGSFrameworkEntities entity = new DGSFrameworkEntities();
        MembershipUser currentUser;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                currentUser = Membership.GetUser();
                if (currentUser == null)
                {
                    Response.Redirect("/Default.aspx");
                }
                LoadProfile();
                LoadOrder();
            }
        }

        protected void btnChangeAccount_Click(object sender, EventArgs e)
        {
            try
            {
                currentUser = Membership.GetUser();
                if (currentUser != null)
                {
                    string uName = currentUser.UserName;
                    MembershipUser someUser = Membership.GetUser(uName);
                    UpdateUser(uName, someUser);
                    lblAlert.Text = "Cập nhật thành công !";
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        private void UpdateUser(string UserName, MembershipUser someUser)
        {
            //sua email
            someUser.Email = tbEmailChange.Text.Trim();
            Membership.UpdateUser(someUser);
            //update thong tin udv_user
            udv_Users user = entity.udv_Users.Where(us => us.UserName == UserName).FirstOrDefault();
            user.Email = tbEmailChange.Text.Trim();
            user.Phone = tbPhoneChange.Text.Trim();
            user.Address = tbAddressChange.Text.Trim();
            entity.SaveChanges();
        }
        protected void ChangePassword1_ChangedPassword(object sender, EventArgs e)
        {
            //create db Log 
            CreateDBLog(null, null, TypeAction.Update, "Cập nhật mật khẩu thành viên", "", "", "", "", "", "", "", "");
            currentUser = Membership.GetUser();
            if (currentUser != null)
            {
                FormsAuthentication.SignOut();
                Response.Redirect("/Default.aspx");
            }
        }
        public void CreateDBLog(int? postID, int? postCateID, TypeAction typeAction, string title, string _abstract, string description,
       string releaseDate, string expireDate, string isApproved, string statusID, string metaDescription, string metaKeywords)
        {
            udv_Log log = new udv_Log();
            log.PostID = postID;
            log.PostCateID = postCateID;
            log.TypeAction = (int)typeAction;
            log.Title = title;
            log.Abstract = _abstract;
            log.Description = description;
            log.ReleaseDate = releaseDate;
            log.ExpireDate = expireDate;
            log.IsApproved = isApproved;
            log.StatusID = statusID;
            log.MetaDescription = metaDescription;
            log.MetaKeywords = metaKeywords;
            log.ActionedDate = DateTime.Now;
            log.ActionBy = Membership.GetUser().UserName;
            log.IpAdress = Globals.GetClientIPAddress();

            entity.AddToudv_Log(log);
            entity.SaveChanges();
        }
        protected void btnCancelChange_Click(object sender, EventArgs e)
        {

        }
        public void LoadProfile()
        {
            currentUser = Membership.GetUser();
            if (currentUser != null)
            {
                var getmem = entity.udv_Users.Where(a => a.UserName == currentUser.UserName && (a.IsActive == true) && (a.ExpiredDate > DateTime.Now)).FirstOrDefault();
                string uName = getmem.UserName;
                tbMemberIdChange.Text = getmem.UserName;
                tbCMNDChange.Text = getmem.PersonID.ToString();
                tbLastNameChange.Text = getmem.LastName;
                tbFirstNameChange.Text = getmem.FirstName;
                tbEmailChange.Text = getmem.Email;
                tbAddressChange.Text = getmem.Address;
                tbPhoneChange.Text = getmem.Phone;
            }
               
        }
        public void LoadOrder()
        {
            try
            {
                currentUser = Membership.GetUser();
                if (currentUser != null)
                {
                    var getOrd = (from o in entity.udv_Orders
                                  join od in entity.udv_OrderDetails on o.OrderID equals od.udv_Orders.OrderID
                                  join u in entity.udv_Users on o.aspnet_Users.UserId equals u.UserID
                                  join p in entity.udv_Products on od.udv_Products.ProductID equals p.ProductID
                                  where o.aspnet_Users.UserName == currentUser.UserName
                                 select new
                                 {
                                     p.Title,
                                     p.Summary,
                                     o.Amount,
                                     o.ExpiredDate,
                                     o.UpdatedDate,
                                     o.RequireDate
                                 }).ToList();
                    rptLoadOrder.DataSource = getOrd;
                    rptLoadOrder.DataBind();
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}